package com.yywq.service.impl;

import com.yywq.error.BusinessException;
import com.yywq.error.EnumBusinessError;
import com.yywq.mapper.OrdersMapper;
import com.yywq.pojo.Orders;
import com.yywq.pojo.view.OrderView;
import com.yywq.service.OrdersService;
import com.yywq.utils.MyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: 吴磊
 * @program: yywq
 * @create: 2021-03-20 22:25
 */
@Service
public class OrdersServiceImpl implements OrdersService {

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private OrderTimeServiceImpl orderTimeService;

    @Autowired
    private MyUtil myUtil;


    @Override
    public List<OrderView> getOrderBySid(String sid) {
        if (StringUtils.isEmpty(sid)) {
            throw new BusinessException(EnumBusinessError.PARAMETER_INVALID_ERROR, "参数不合法");
        }
        List<Orders> sorders = ordersMapper.getOrdersBySid(sid);
        List<OrderView> OrderViews = sorders.stream().map(sorder -> {
            OrderView orderView = new OrderView();
            BeanUtils.copyProperties(sorder, orderView);
            orderView.setSendTime(orderTimeService.getOrderTimeByOid(orderView.getId()).getSendTime());
            return orderView;
        }).collect(Collectors.toList());

        return OrderViews;
    }

    @Override
    public List<OrderView> getOrderByBid(String bid) {
        if (StringUtils.isEmpty(bid)) {
            throw new BusinessException(EnumBusinessError.PARAMETER_INVALID_ERROR, "参数不合法");
        }
        List<Orders> sorders = ordersMapper.getOrdersByBid(bid);
        List<OrderView> OrderViews = sorders.stream().map(sorder -> {
            OrderView orderView = new OrderView();
            BeanUtils.copyProperties(sorder, orderView);
            orderView.setSendTime(orderTimeService.getOrderTimeByOid(orderView.getId()).getSendTime());
            return orderView;
        }).collect(Collectors.toList());

        return OrderViews;
    }

    /**
     * 事务处理
     *
     * @param bid       买家ID
     * @param sid       卖家ID
     * @param addressId 买家的收获地址
     * @param price     成交价格
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = BusinessException.class)
    @Override
    public int generateOrders(String bid, String sid, String addressId, String gid, BigDecimal price) {
        if (StringUtils.isEmpty(bid)) {
            throw new BusinessException(EnumBusinessError.PARAMETER_INVALID_ERROR, "买家id不能为空");
        }
        if (StringUtils.isEmpty(sid)) {
            throw new BusinessException(EnumBusinessError.PARAMETER_INVALID_ERROR, "卖家id不能为空");
        }
        if (StringUtils.isEmpty(addressId)) {
            throw new BusinessException(EnumBusinessError.PARAMETER_INVALID_ERROR, "买家地址id不能为空");
        }
        if (price.doubleValue() < 0) {
            throw new BusinessException(EnumBusinessError.PARAMETER_INVALID_ERROR, "交易金额不能小于0");
        }
        String id = MyUtil.getUUID();
        String payTime = MyUtil.getCurrentTime();
        Orders orders = new Orders();
        orders.setId(id);
        orders.setBid(bid);
        orders.setSid(sid);
        orders.setAddressId(addressId);
        orders.setGid(gid);
        orders.setPayTime(payTime);
        orders.setPrice(price);
        int i = ordersMapper.insertOrder(orders);
        if (i < 0) {
            throw new BusinessException(EnumBusinessError.UNKNOWN_ERROR, "插入数据库时候失败");
        }
        return i;
    }


}
